package org.openkanban.client.ui.presenter;

import org.openkanban.client.ui.board.ActivityView;
import org.openkanban.shared.dto.KanbanDto;

import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.Response;
import com.google.gwt.json.client.JSONArray;
import com.smartgwt.client.util.JSON;
import com.smartgwt.client.util.SC;

/**
 * Callback that handles getkanbans request
 */
public class GetKanbansByActivityIdRequestCallback implements RequestCallback {

    private ActivityView activityView;

    public GetKanbansByActivityIdRequestCallback(ActivityView activityView) {
	this.activityView = activityView;
    }

    @Override
    public void onResponseReceived(Request request, Response response) {
	if (response.getStatusCode() < 300) {
	    JSONArray jsa = new JSONArray(JSON.decode(response.getText()));
	    KanbanDto kanbanDto;
	    activityView.getKanbanContainerVLayout().removeMembers(activityView.getKanbanContainerVLayout().getMembers());
	    for (int i = 0; i < jsa.size(); i++) {
		kanbanDto = new KanbanDto(jsa.get(i).isObject().toString());
		activityView.addKanban(kanbanDto);
	    }
	} else {
	    SC.say("Fehler: " + response.getStatusText());
	}
    }

    @Override
    public void onError(Request request, Throwable exception) {
    }

}
